package com.sright.bcard.enterprise.web.config;

import com.sright.bcard.enterprise.web.context.ContextWare;
import com.sright.bcard.enterprise.web.context.ServletProxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author peter
 * @description
 * @date 2020/6/2
 */
@Configuration
public class WebConfiguration implements ApplicationContextAware, SmartInitializingSingleton {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterSingletonsInstantiated() {
        ServletProxy servletProxy = new ServletProxy();
        Map<String, ContextWare> map = applicationContext.getBeansOfType(ContextWare.class);
        for (Map.Entry<String, ContextWare> entry: map.entrySet()) {
            ContextWare contextWare = entry.getValue();
            contextWare.setContext(servletProxy);
        }
    }
}
